package com.tdam2013.grupo12.webMessaging;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;







import org.xml.sax.SAXException;

import com.tdam2013.grupo12.NombreUsuario;
import com.tdam2013.grupo12.broadcastReceiver.InformacionRed;

import android.os.AsyncTask;
import android.util.Log;

public class RegistrarUsuario extends AsyncTask<String, Void, String> {

	private StringBuilder responseStr;
	private HttpClient client = null;
	private HttpPost request = null;
	private HttpResponse response = null;
	private InputStream in;
	private RegistrarUsuarioHandler handler;
	private NombreUsuario nombreU;
	
	public RegistrarUsuario(NombreUsuario nombreU){
		super();
		this.nombreU = nombreU;
	}
	
	@Override
	protected String doInBackground(String... params) {
		String resultado = this.registrarUsuario(params[0], params[1]);
		return resultado;
	}
	
	public String registrarUsuario(String username, String password){
		try {
			responseStr = new StringBuilder();
			client = new DefaultHttpClient();
		      String url="http://"+InformacionRed.ip+"/MessageSender/";
		      request = new HttpPost(url);
		      StringEntity body= new StringEntity(String.format("<action id=\"%s\" name=\"register-user\"><action-detail><user username=\"%s\" password=\"%s\"/></action-detail></action>", Calendar.getInstance().getTimeInMillis(), username, password), "ISO-8859-1");
		      request.setEntity(body);
		      response = client.execute(request);
		      in = response.getEntity().getContent();
		      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		      String line = null;
		      while ((line = reader.readLine()) != null) 
		      {
		        responseStr.append(line + "\n");
		      }
		}
		catch (Exception e) {
			Log.e("La renga","Exception"+e.getMessage());
		}
		Log.e("Hola", responseStr.toString());
		return  responseStr.toString();
	}
	
	@Override
	protected void onPostExecute(String resultado){
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		try {
			handler = new RegistrarUsuarioHandler();
			SAXParser parser = saxParserFactory.newSAXParser();
			parser.parse(new ByteArrayInputStream(resultado.getBytes()), handler);
			this.nombreU.setResultado(handler.getResultado());
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			Log.e("MOMO" , e.getMessage());
		}
	}
	
	
}
